package com.example.oasystem.controller.user;

import com.example.oasystem.dao.login.AoaUserDao;
import com.example.oasystem.dao.position.AoaPositionDao;
import com.example.oasystem.dao.role.AoaRoleDao;
import com.example.oasystem.entity.dept.AoaDept;
import com.example.oasystem.entity.page.Page;
import com.example.oasystem.entity.position.AoaPosition;
import com.example.oasystem.entity.role.AoaRole;
import com.example.oasystem.entity.user.AoaUser;
import com.example.oasystem.entity.user.AoaUserLoginRecord;
import com.example.oasystem.service.AoaDeptService;
import com.example.oasystem.service.AoaUserService;
import com.example.oasystem.utils.PinYinUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class UserManageController {
    String sousuo=null;
    @Autowired
    AoaUserDao aoaUserDao;
    @Autowired
    AoaDeptService aoaDeptService;
    @Autowired
    AoaUserService aoaUserService;
    @Autowired
    AoaPositionDao aoaPositionDao;
    @Autowired
    AoaRoleDao aoaRoleDao;
    @RequestMapping("/usermanage")
    public String usermanage(HttpServletRequest request, Model model, @RequestParam(value = "usersearch",required = false,defaultValue = "") String usersearch,@RequestParam(value = "page", required = false,defaultValue = "0") int number){
        AoaUser sessionUser=(AoaUser) request.getSession().getAttribute("user");
        if (usersearch.equals(""))
        {
            usersearch=null;
            sousuo=null;

        }
        else
        {
            sousuo=usersearch;
        }
        List<AoaUser> quan=aoaUserDao.getAoaUserListByDeptId(sessionUser.getDeptId().toString(),usersearch);
        int totalElement= quan.size();
        int size=4;
        int totalPages=totalElement%size==0? totalElement/size:totalElement/size+1;

        boolean first;
        if (number==0)
        {
            first=true;
        }
        else
        {
            first=false;
        }
        boolean last;
        if (number==(totalPages-1))
        {
            last=true;
        }
        else
        {
            last=false;
        }
        Page page=new Page(totalElement,size,totalPages,number, first,last);
        List<AoaUser> list=aoaUserDao.getAoaUserListByDeptIdLimit(sessionUser.getDeptId().toString(),usersearch,(number)*size,size);
        if(usersearch==null)
        {
            usersearch="";

        }
        System.out.println("page = " + page);
        model.addAttribute("usersearch",usersearch);
        model.addAttribute("page",page);
        model.addAttribute("url","chazhaoForUser");
        model.addAttribute("users",list);
        return "user/usermanage";
    }
    @RequestMapping("/chazhaoForUser")
    public String chazhao(int page,HttpServletRequest request, Model model){
        System.out.println("进来了");
        AoaUser sessionUser=(AoaUser) request.getSession().getAttribute("user");
        List<AoaUser> quan=aoaUserDao.getAoaUserListByDeptId(sessionUser.getDeptId().toString(),sousuo);
        int totalElement= quan.size();
        int size=4;
        int totalPages=totalElement%size==0? totalElement/size:totalElement/size+1;
        int number=page;
        boolean first;
        if (number==0)
        {
            first=true;
        }
        else
        {
            first=false;
        }
        boolean last;
        if (number==(totalPages-1))
        {
            last=true;
        }
        else
        {
            last=false;
        }
        Page fenye=new Page(totalElement,size,totalPages,number, first,last);
        List<AoaUser> list=aoaUserDao.getAoaUserListByDeptIdLimit(sessionUser.getDeptId().toString(),sousuo,(number)*size,size);
        model.addAttribute("users",list);
        model.addAttribute("usersearch",sousuo);
        model.addAttribute("page",fenye);
        model.addAttribute("url","chazhaoForUser");
        return "user/usermanagepaging";
    }
    @RequestMapping("/deleteuser")
    public String deleteuser(Long userid, HttpServletRequest request, Model model,@RequestParam("page") int page){
        aoaUserDao.delById(userid);
        System.out.println("删除id为"+userid);
        return "forward:/usermanage?page="+page;
    }

    @GetMapping("useredit")
    public String toEditUserGet(@RequestParam(value = "userid",required = false ,defaultValue = "0" ) long userid,Model model,HttpSession session,@RequestParam("page") int page){
        System.out.println("get新增");
        AoaUser aoaUser = new AoaUser();
        //获取session中的user
        AoaUser user =(AoaUser) session.getAttribute("user");
        //分权限获得可以选择的部门
        List<AoaDept> depts = aoaDeptService.selectByRole(user);
        //获取当前修改的用户的部门可以选择的职位
        List<AoaPosition> positions = aoaPositionDao.selectByDeptId(user.getPositionId());

        //分权限获得角色
        List<AoaRole> roles = aoaRoleDao.selectByRole(user.getRoleId());

        if(userid == 0){
            //新增页面
            aoaUser.setAoaDept(new AoaDept());
            aoaUser.setAoaRole(new AoaRole());
            aoaUser.setAoaPosition(new AoaPosition());
            System.out.println(aoaUser);
            model.addAttribute("user",aoaUser);
        }else{
            //修改页面
            aoaUser = aoaUserDao.selectByUserId(userid);
            positions = aoaPositionDao.selectByDeptId(aoaUser.getDeptId());
            model.addAttribute("user",aoaUser);
        }
        //存值
        System.out.println(depts);
        System.out.println(positions);
        System.out.println(roles);

        model.addAttribute("depts",depts);
        model.addAttribute("positions",positions);
        model.addAttribute("roles",roles);
        System.out.println("进入修改新增页面时的page"+page);
        model.addAttribute("page",page);
        return "user/edituser";
    }

    @PostMapping("useredit")
    public String toEditUserPost(Model model,HttpSession session,AoaUser userNew,@RequestParam("page") int page){
        System.out.println("进行保存时的page"+page);

        AoaUser aoaUser = new AoaUser();
        System.out.println("userNew = " + userNew);
        if(userNew.getUserId()==null){
            //新增用户
            String userIdcard = userNew.getUserIdcard();
            userNew.setBirth(userIdcard.substring(6, 10) + "-"
                    + userIdcard.substring(10, 12) + "-"
                    + userIdcard.substring(12, 14));
            userNew.setFatherId(aoaUserService.selectFartherIdByDeptAndPosition(userNew.getDeptId(),userNew.getRoleId()));
            userNew.setImgPath("oasys.jpg");
            userNew.setPassword("123456");
            userNew.setSuperman(0);
            userNew.setIsLock(0);
            userNew.setPinyin(PinYinUtil.getPinyin(userNew.getUserName()));
            System.out.println(userNew);
            int insert = aoaUserDao.insert(userNew);
            if(insert==1){
                return "forward:usermanage";
            }
        }else {
            //修改用户
            int i = aoaUserDao.updateById(userNew);
            return "forward:/usermanage?page="+page;
        }

        return null;
    }
    @RequestMapping("selectdeptOnUserManage")
    @ResponseBody
    public List<AoaPosition> selectDept(long selectdeptid){
        List<AoaPosition> positions = aoaPositionDao.selectByDeptId(selectdeptid);
        return positions;
    }
}
